package com.soft.mainsite.common.utils;/**
 * @(#)2019/4/19 8:44
 * <p>
 * Copyright HeNan soft. All rights reserved.
 */

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>此类用于Date CRON表达式互转</p>
 * <p>@author:xuzhenjia</p>
 * <p>@date:2019/4/19</p>
 * <p>@remark</p>
 */
public class CronDateUtil {
    private static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";

    /***
     *
     * @param date 时间
     * @return cron类型的日期
     */
    public static String getCron(final Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
        String formatTimeStr = "";
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /***
     *
     * @param cron Quartz cron的类型的日期
     * @return Date日期
     */
    public static Date getDate(final String cron) {


        if (cron == null) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
        Date date = null;
        try {
            date = sdf.parse(cron);
        } catch (Exception e) {
            // 此处缺少异常处理,自己根据需要添加
            return null;
        }
        return date;
    }

    public static void main(String[] args) {
        Date now = new Date();
        System.out.println(CronDateUtil.getCron(now));

        String cron = "20 28 17 02 08 ? 2016";

        Date cronDate = CronDateUtil.getDate(cron);
        System.out.println("===================");
        System.out.println(cronDate.toString());


    }

}
